﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.FieldList;
using Telerik.Windows.Controls.SyntaxEditor.Folding;
using Telerik.Windows.Controls.SyntaxEditor.Taggers;
using Telerik.Windows.SyntaxEditor.Core.Tagging;
using Telerik.Windows.SyntaxEditor.Core.Text;

namespace Chaotic.Module.DBTool.Common
{
    internal class SqlFoldingTagger :  FoldingTaggerBase
    {
        private readonly RadSyntaxEditor _editor; 
        public SqlFoldingTagger(RadSyntaxEditor editor):base(editor)
        {
            _editor = editor;
            base.FoldingRegionDefinitions.Add(new FoldingRegionDefinition("#region", "#endregion"));
            base.FoldingRegionDefinitions.Add(new FoldingRegionDefinition("begin", "end"));
        }

        protected override void RebuildFoldingRegions()
        {
            base.RebuildFoldingRegions();
            BuildNewLineFolderRegion();
        }

        //换行即表示需要折叠
        private void BuildNewLineFolderRegion()
        {
            var text = _editor.Document.CurrentSnapshot.GetText();
            var lines = text.Split('\n');
            var regions = new Stack<int>();
            //只要有空行 就代表一个代码块的结束
            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Trim();
                if (string.IsNullOrWhiteSpace(line))
                {
                    if (regions.Count > 0)
                    {
                        var startLine = regions.Pop();

                        //如果开始行和结束行相等 则不处理
                        if (startLine == i - 1)
                            continue;
                        TextSnapshotLine textSnapshotLine = _editor.Document.CurrentSnapshot.Lines.FirstOrDefault(o => o.LineNumber == startLine);
                        var startOffset = textSnapshotLine.Span.Start;
                        TextSnapshotLine iSnapshotLine = _editor.Document.CurrentSnapshot.Lines.FirstOrDefault(o => o.LineNumber == i - 1);
                        var endOffset = iSnapshotLine.Span.End;

                        if (startOffset < endOffset)
                        {
                            var span = new TextSnapshotSpan(_editor.Document.CurrentSnapshot, new Span(startOffset, endOffset - startOffset));
                            //获取第一行作为tag
                            string tagName = textSnapshotLine.GetText().Trim();
                            base.FoldingRegionTags.Add(new TagSpan<FoldingRegionTag>(span, new FoldingRegionTag(tagName, false, null)));
                        }
                    }
                }
                else
                {
                    if (regions.Count == 0)
                        regions.Push(i);
                }
            }
        }

        //public override IEnumerable<TagSpan<FoldingRegionTag>> GetTags(NormalizedSnapshotSpanCollection spans)
        //{
        //    var tags = new List<TagSpan<FoldingRegionTag>>();
        //    var text = _editor.Document.CurrentSnapshot.GetText();

        //    // 识别#region/#endregion对
        //    var regions = new Stack<int>();

        //    var lines = text.Split('\n');

        //    //只要有空行 就代表一个代码块的结束
        //    for (int i = 0; i < lines.Length; i++)
        //    {
        //        var line = lines[i].Trim();
        //        if ( string.IsNullOrWhiteSpace(line) ) {
        //            if ( regions.Count > 0 ) {
        //                var startLine = regions.Pop();

        //                //如果开始行和结束行相等 则不处理
        //                if(startLine == i - 1)
        //                    continue;
        //                TextSnapshotLine textSnapshotLine = _editor.Document.CurrentSnapshot.Lines.FirstOrDefault(o => o.LineNumber == startLine);
        //                var startOffset = textSnapshotLine.Span.Start;
        //                TextSnapshotLine iSnapshotLine = _editor.Document.CurrentSnapshot.Lines.FirstOrDefault(o => o.LineNumber == i - 1);
        //                var endOffset = iSnapshotLine.Span.End;

        //                if (startOffset < endOffset)
        //                {
        //                    var span = new TextSnapshotSpan(_editor.Document.CurrentSnapshot, new Span(startOffset, endOffset - startOffset));
        //                    //获取第一行作为tag
        //                    string tagName = textSnapshotLine.GetText().Trim();
        //                    tags.Add(new TagSpan<FoldingRegionTag>(span, new FoldingRegionTag(tagName, false , null)));
        //                }
        //            }
        //        } 
        //        else
        //        {
        //            if (regions.Count == 0)
        //                regions.Push(i);
        //        }
        //    } 
        //    return tags;
        //} 
    }

    public class FoldingTag :ITag
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public FoldingTag()
        {
        }
    }
}
